﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Data.SqlClient;

public partial class _Student : System.Web.UI.Page
{
    //学生页面初始化
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            studentName.Text = ((Cstudent)Session["studentData"]).studentName;
            //第一次页面请求执行默认显示个人信息页面
            if (!IsPostBack)
            {
                Button_studentData_Click(sender, e);
            }
            
        }
        catch (Exception myE)
        {
            Server.Transfer("Default.aspx");
        }
        //添加新闻事件
        CourseDataList.ItemCommand +=
             new DataGridCommandEventHandler(this.showCourseInfo);
        MYCOURSELIST.ItemCommand +=
             new DataGridCommandEventHandler(this.ItemsGrid_Delete);
    }
    //学生查看个人信息
    protected void Button_studentData_Click(object sender, EventArgs e)
    {
        showHgc(StudentData);
        Cstudent stu = (Cstudent)Session["studentData"];
        Num.Text = stu.studentID;
        Name.Text = stu.studentName;
        Sex.Text = stu.studentSex;
        College.Text = stu.studentCollege;
        IDcardNo.Text = stu.studentIDcardNo;
        Major.Text = stu.studentMajor;
        Class.Text = stu.studentClass.ToString();
        Levels.Text = stu.studentLevels;
        courseNumMax.Text = stu.courseNumMax.ToString();       
    }
    //学生进入密码修改
    protected void Button_changePassword_Click(object sender, EventArgs e)
    {
        showHgc(ChangePassword);
    }
    //密码修改
    protected void Button_CPOK_Click(object sender, EventArgs e)
    {
        if (!newPassword1.Text.Equals(newPassword2.Text))
        {
            ClientScript.RegisterStartupScript(typeof(string), "aa", "<script>alert(\"2次密码不匹配！！\")</script>");
            return;
        }

        Cstudent stu = (Cstudent)Session["studentData"];
        if (oldPassword.Text.PadRight(50).Equals(stu.studentPassword))
        {

            String sqlChangePassword = "UPDATE student SET studentPassword = '" + newPassword1.Text +
                                    "' WHERE studentID = '" + stu.studentID + "'";
            DataBase.ExecuteSQL(sqlChangePassword);
            DataBase.closeConn();
            ClientScript.RegisterStartupScript(typeof(string), "aa", "<script>alert(\"密码修改成功！！\")</script>");
            ChangePassword.Visible = false;
            return;
        }
        else
        {
            ClientScript.RegisterStartupScript(typeof(string), "aa", "<script>alert(\"原始密码不匹配！！\")</script>");
            return;
        }
    }
    //查看课程列表
    protected void Button_allCourse_Click(object sender, EventArgs e)
    {
        showHgc(DIVCourseDataList);
        String coursesql = "select * from course INNER JOIN dept ON course.dept = dept.deptID INNER JOIN teacher ON course.teacher = teacher.teacherID";       
        DataSet courseDataSet = DataBase.Query(coursesql);
        CourseDataList.DataSource = courseDataSet.Tables[0].DefaultView;
        CourseDataList.DataBind();
    }
    //选择教学单位后显示课程列表
    protected void DropDownList_dept_SelectedIndexChanged(object sender, EventArgs e)
    {
        showHgc(DIVCourseDataList);
        String coursesql = "select * from course INNER JOIN dept ON course.dept = dept.deptID INNER JOIN teacher ON course.teacher = teacher.teacherID where dept.deptName = '" + DropDownList_dept.SelectedValue + "'";
        try
        {
            DataSet courseDataSet = DataBase.Query(coursesql);
            CourseDataList.DataSource = courseDataSet.Tables[0].DefaultView;
            CourseDataList.DataBind();
        }
        catch (Exception myE)
        {
            throw myE;
        }
        finally
        {
            DataBase.closeConn();
        }
    }
    //查看自己选择的课程
    protected void Button_myCourse_Click(object sender, EventArgs e)
    {
        showHgc(DIVMYCOURSELIST);
        String coursesql = "SELECT courseName,period,credit,term,time,site,score "+
                            "FROM stu_course INNER JOIN student ON stu_course.student = student.studentID INNER JOIN "+
                            "course ON stu_course.course = course.courseID where studentID = '" + ((Cstudent)Session["studentData"]).studentID + "'";
        try
        {
            DataSet courseDataSet = DataBase.Query(coursesql);
            MYCOURSELIST.DataSource = courseDataSet.Tables[0].DefaultView;
            MYCOURSELIST.DataBind();
        }
        catch (Exception myE)
        {
            throw myE;
        }
        finally
        {
            DataBase.closeConn();
        }
    }
    //删除自己选择的课程事件
    protected void ItemsGrid_Delete(Object sender, DataGridCommandEventArgs e)
    {
        switch (((LinkButton)e.CommandSource).CommandName)
        {
            case "deleteCourse":
                deletecourse(sender, e);
                break;
            default:
                break;

        }

    }
    //删除课程
    protected void deletecourse(object sender, DataGridCommandEventArgs e)
    {
        string courseName = e.Item.Cells[0].Text;
        //通过课程名称获取课程ID
        string selectCourseIDSql = "select courseID from course where courseName = '" + courseName + "'";
        try
        {
            SqlDataReader dataReader = DataBase.ExecuteReader(selectCourseIDSql);
            if (dataReader.Read() == false)
            {
                return;
            }
            else
            {
                int courseID = dataReader.GetInt32(0);
                string studentID = ((Cstudent)Session["studentData"]).studentID;
                //通过课程ID和学生ID删除此对应成绩单
                string deleteSql = "delete stu_course where student = '" + studentID + "' and course = '" + courseID + "'";
                DataBase.ExecuteSQL(deleteSql);
                DataBase.closeConn();
                string msg = "删除成功";
                ClientScript.RegisterStartupScript(typeof(string), "aa", "<script>alert('" + msg + "')</script>");
                Button_myCourse_Click(sender, e);
            }
        }
        catch (Exception myE)
        {
            throw myE;
        }
        finally
        {
            DataBase.closeConn();
        }
      
    }
    //用户退出
    protected void Exit_Click(object sender, EventArgs e)
    {
        Session.Clear();
        Server.Transfer("Default.aspx");
    }
    //触发查看课程信息事件
    protected void showCourseInfo(Object sender, DataGridCommandEventArgs e)
    {
        switch (((LinkButton)e.CommandSource).CommandName)
        {
            case "showCourse":
                courseInfo(sender, e);
                break;
            case "addCourse":
                addCourse(sender, e);
                break;
            default:
                break;

        }

    }
    //查看课程信息
    protected void courseInfo(object sender, DataGridCommandEventArgs e)
    {
        showHgc(DivCourseInfo);
        LinkButton selectButton = (LinkButton)e.CommandSource;
        String courseName = selectButton.Text;
        String sql = "SELECT courseID,courseName,teacherName,period,credit,stuNumMax,deptName,time,site,term,introduce " +
                       "FROM course INNER JOIN dept ON course.dept = dept.deptID INNER JOIN  teacher ON course.teacher = teacher.teacherID where courseName = '" + courseName.Trim() + "'";
        try
        {
            SqlDataReader dataReader = DataBase.ExecuteReader(sql);
            if (dataReader.Read() == false)
            {
                Response.Write("<script>alert(\"此课程不存在！\")</script>");
                Response.Write("<script>window.close('course.aspx')</script>");
                return;
            }
            else
            {
                LabelCourseID.Text = dataReader.GetInt32(0).ToString();
                LabelCourseName.Text = dataReader.GetString(1);
                LabelTeacherName.Text = dataReader.GetString(2);
                LabelPeriod.Text = dataReader.GetInt32(3).ToString();
                LabelCredit.Text = dataReader.GetInt32(4).ToString();
                LabelMaxNum.Text = dataReader.GetInt32(5).ToString();
                LabelDept.Text = dataReader.GetString(6);
                LabelTime.Text = dataReader.GetString(7);
                LabelSite.Text = dataReader.GetString(8);
                LabelTerm.Text = dataReader.GetString(9);
                LabelIntroduce.Text = dataReader.GetString(10);
            }
        }
        catch(Exception myE)
        {
            throw myE;
        }
        finally
        {
            DataBase.closeConn();
        }
    }
    //课程列表页面选择课程
    protected void addCourse(object sender, DataGridCommandEventArgs e)
    {
        //判断学生是否欠费能否选课
        if (((Cstudent)Session["studentData"]).status == 0)
        {
            ClientScript.RegisterStartupScript(typeof(string), "aa", "<script>alert(\"对不起，欠费学生不能进行选课功能！\")</script>");
        }
        else
        {
            string courseID = e.Item.Cells[0].Text;
            string courseName = ((LinkButton)e.Item.Cells[1].Controls[0]).Text;
            string studentID = ((Cstudent)Session["studentData"]).studentID.Trim();
            string sql1 = "select * from stu_course where student ='" + studentID + "' and course = '" + courseID + "'";
            try
            {
                if (DataBase.ExecuteReader(sql1).Read() == true)
                {
                    ClientScript.RegisterStartupScript(typeof(string), "aa", "<script>alert(\"您已经选择该课程,请勿重复选择！\")</script>");
                    Button_myCourse_Click(sender, e);
                }
                else
                {
                    String sql2 = "insert into stu_course (student, course) values ('" + studentID + "','" + courseID + "')";
                    DataBase.ExecuteSQL(sql2);
                    ClientScript.RegisterStartupScript(typeof(string), "aa", "<script>alert(\"恭喜,你已经成功选择了 " + courseName.Trim() + " ！\")</script>");
                    Button_myCourse_Click(sender, e);
                }
            }
            catch (Exception myE)
            {
                throw myE;
            }
            finally
            {
                DataBase.closeConn();
            }

        }
    }
    //课程信息页面选择课程
    protected void Button_ChooseCourse_Click(object sender, EventArgs e)
    {
                //判断学生是否欠费能否选课
        if (((Cstudent)Session["studentData"]).status == 0)
        {
            ClientScript.RegisterStartupScript(typeof(string), "aa", "<script>alert(\"对不起，欠费学生不能进行选课功能！\")</script>");
        }
        else
        {
            String courseID = LabelCourseID.Text.Trim();
            String studentID = ((Cstudent)Session["studentData"]).studentID.Trim();
            String sql1 = "select * from stu_course where student ='" + studentID + "' and course = '" + courseID + "'";
            try
            {
                if (DataBase.ExecuteReader(sql1).Read() == true)
                {
                    ClientScript.RegisterStartupScript(typeof(string), "aa", "<script>alert(\"您已经选择该课程,请勿重复选择！\")</script>");
                    Button_myCourse_Click(sender, e);
                }
                else
                {
                    String sql2 = "insert into stu_course (student, course) values ('" + studentID + "','" + courseID + "')";
                    DataBase.ExecuteSQL(sql2);
                    ClientScript.RegisterStartupScript(typeof(string), "aa", "<script>alert(\"恭喜,你已经成功选择了 " + LabelCourseName.Text.Trim() + " ！\")</script>");
                    Button_myCourse_Click(sender, e);
                }
            }
            catch (Exception myE)
            {
                throw myE;
            }
            finally
            {
                DataBase.closeConn();
            }
        }
    }
    //界面控制
    protected void showHgc(HtmlGenericControl hgc)
    {
        ChangePassword.Visible = false;
        DIVCourseDataList.Visible = false;
        DIVMYCOURSELIST.Visible = false;
        StudentData.Visible = false;
        DivCourseInfo.Visible = false;
        hgc.Visible = true;
    }
}
